package com.qwz.example;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Qiuwuzhi
 * @version V1.0
 * @Description: 享元工厂，通常实现完单例
 * @Package com.qwz.example.FlyWeightFactory
 * @date 2019/12/23 15:57
 */
public class FlyWeightFactory {

    private static FlyWeightFactory factory = new FlyWeightFactory();

    private FlyWeightFactory() {
    }

    public static FlyWeightFactory getInstance() {
        return factory;
    }

    private Map<String, FlyWeight> map = new HashMap<String, FlyWeight>();

    public FlyWeight getFlyWeight(String key) {
        FlyWeight flyWeight = map.get(key);
        if (flyWeight == null) {
            flyWeight = new AuthorizationFlyWeight(key);
            map.put(key, flyWeight);
        }
        return flyWeight;
    }
}
